//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CovarianceDecomposition.h"
using namespace Cephei::QL::Math::Matrixutilities;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (Cephei::QL::Math::IMatrix^ covarianceMatrix, Microsoft::FSharp::Core::FSharpOption<Double>^ tolerance) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _CcovarianceMatrix;
    try
    {
#ifdef HANDLE
        _phCovarianceDecomposition = NULL;
#endif
        _CcovarianceMatrix = safe_cast<CMatrix^> (covarianceMatrix);
        _CcovarianceMatrix->Lock();
        QuantLib::Matrix& _covarianceMatrix = static_cast<QuantLib::Matrix&> (_CcovarianceMatrix->GetReference ()); 
        QuantLib::Real _tolerance = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (tolerance) ? (QuantLib::Real)ValueHelper::Convert (tolerance->Value) : 1.0e-12); //4
        _ppCovarianceDecomposition = new boost::shared_ptr<QuantLib::CovarianceDecomposition> (new QuantLib::CovarianceDecomposition ( _covarianceMatrix,  _tolerance ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcovarianceMatrix != nullptr) _CcovarianceMatrix->Unlock();
    }
}
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (boost::shared_ptr<QuantLib::CovarianceDecomposition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCovarianceDecomposition = NULL;
#endif
	_ppCovarianceDecomposition = &childNative;
    
}
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (QuantLib::CovarianceDecomposition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCovarianceDecomposition = NULL;
#endif
	_ppCovarianceDecomposition = new boost::shared_ptr<QuantLib::CovarianceDecomposition> (&childNative);
    
    _CovarianceDecompositionOwner = owner;
    
}

Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (CCovarianceDecomposition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCovarianceDecomposition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCovarianceDecomposition = new boost::shared_ptr<QuantLib::CovarianceDecomposition> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCovarianceDecomposition = NULL;
#endif
	if (!t->IsSubclassOf(CCovarianceDecomposition::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (QuantLib::Handle<QuantLib::CovarianceDecomposition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCovarianceDecomposition = &childNative;
	_ppCovarianceDecomposition = &static_cast<boost::shared_ptr<QuantLib::CovarianceDecomposition>>(childNative.currentLink());
    
    _CovarianceDecompositionOwner = owner;
}
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (QuantLib::Handle<QuantLib::CovarianceDecomposition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCovarianceDecomposition = &childNative;
	_ppCovarianceDecomposition = &static_cast<boost::shared_ptr<QuantLib::CovarianceDecomposition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CCovarianceDecomposition (QuantLib::CovarianceDecomposition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCovarianceDecomposition = NULL;
#endif
	_ppCovarianceDecomposition = new boost::shared_ptr<QuantLib::CovarianceDecomposition> (new QuantLib::CovarianceDecomposition (childNative));
    
}
#endif

Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::~CCovarianceDecomposition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCovarianceDecomposition != NULL)
    {
	    delete _ppCovarianceDecomposition;
        _ppCovarianceDecomposition = NULL;
    }
}
Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::!CCovarianceDecomposition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCovarianceDecomposition != NULL)
    {
	    delete _ppCovarianceDecomposition;
    }
}
QuantLib::CovarianceDecomposition& Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::GetReference ()
{
    if (_ppCovarianceDecomposition == NULL) throw gcnew NativeNullException ();
	return **_ppCovarianceDecomposition;
}
boost::shared_ptr<QuantLib::CovarianceDecomposition>& Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::GetShared ()
{
    if (_ppCovarianceDecomposition == NULL) throw gcnew NativeNullException ();
	return *_ppCovarianceDecomposition;
}
QuantLib::CovarianceDecomposition* Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::GetPointer ()
{
    if (_ppCovarianceDecomposition == NULL) throw gcnew NativeNullException ();
	return &**_ppCovarianceDecomposition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CovarianceDecomposition>& Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::GetHandle ()
{
	if (_phCovarianceDecomposition == NULL)
	{
		_phCovarianceDecomposition = new Handle<QuantLib::CovarianceDecomposition> (*_ppCovarianceDecomposition);
	}
	return *_phCovarianceDecomposition;
}
#endif
bool Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::HasNative () 
{
	return (_ppCovarianceDecomposition != NULL);
}

Cephei::QL::Math::IMatrix^ Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::CorrelationMatrix::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppCovarianceDecomposition)->correlationMatrix ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::StandardDeviations::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppCovarianceDecomposition)->standardDeviations ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition::Variances::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppCovarianceDecomposition)->variances ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Matrixutilities::ICovarianceDecomposition^ Cephei::QL::Math::Matrixutilities::CCovarianceDecomposition_Factory::Create (Cephei::QL::Math::IMatrix^ covarianceMatrix, Microsoft::FSharp::Core::FSharpOption<Double>^ tolerance)
{
    return gcnew CCovarianceDecomposition ( covarianceMatrix,  tolerance);
}
